package com.shengqiang.zhenhuiTransfer.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.shengqiang.zhenhuiTransfer.entity.Order;
import com.shengqiang.zhenhuiTransfer.mapper.OrderMapper;
import com.shengqiang.zhenhuiTransfer.entity.Result;
import com.shengqiang.zhenhuiTransfer.utils.DateUtils;
import com.sun.org.apache.xpath.internal.axes.MatchPatternIterator;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderService extends ServiceImpl<OrderMapper,Order>{

    private static final Logger logger= LoggerFactory.getLogger(OrderService.class);

    Gson gson=new Gson();


    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    /**
     * 添加订单
     * @param order
     * @return
     */
    public Result<String> addOrder(Order order){
        try{
            order.setOrderId(System.currentTimeMillis()+"");
            order.setCreateTime(dateFormat.format(new Date()));
            baseMapper.insert(order);
            return new Result<>(0,"success");
        }catch (Exception ex){
            logger.error("添加订单出现异常:{},order={}",ex,gson.toJson(order));
            return new Result<>(1,"fail");
        }
    }

    /**
     * 修改订单
     * @param order
     * @return
     */
    public Result<String> updateOrder(Order order){
        try{
            baseMapper.updateOrder(order);
            return new Result<>(0,"success");
        }catch (Exception ex){
            logger.error("修改订单出现异常:{},order={}",ex,order);
            return new Result<>(1,"fail");
        }
    }

    public Result<List<Order>> queryCurrenDayOrders(String date,String openId){
        try{
            Map<String,String> params= Maps.newHashMap();
            params.put("date",date);
            params.put("openId",openId);
            List<Order> orders=baseMapper.queryCurrency(params);
            if(orders==null){
                logger.error("查询今日订单返回异常,date={},openId={}");
                return new Result<>(1,"fail");
            }
            return new Result<>(0,"success",orders);
        }catch (Exception ex){
            logger.error("查询今日订单出现异常:{},date={},openId={}",ex,date,openId);
            return new Result<>(1,"fail");
        }
    }

    public Result<Map<String,Object>> queryMonthOrders(String date,String openId){
        try{
            Map<String,String> params= Maps.newHashMap();
            params.put("beginDate",date+"-01");
            params.put("endDate", DateUtils.getMonthMaxDay(date));
            params.put("openId",openId);
            List<Order> orders=baseMapper.queryMonthOrders(params);
            Map<String,Object> result=Maps.newHashMap();
            double repairCosts=0;
            double transportation=0;
            double receivedTotal=0;
            double advanceTotal=0;
            double oilCostTotal=0;
            double otherCostTotal=0;
            double total=0;

            Map<String,List<Order>> dayOrders=Maps.newHashMap();
            for (Order order:orders){

                if(order.getType()==0){
                    transportation=transportation+order.getTotal();
                    receivedTotal=receivedTotal+order.getReceived();
                    advanceTotal=advanceTotal+order.getAdvance();
                }else if(order.getType()==1){//修理费
                    repairCosts=repairCosts+order.getTotal();
                }else if(order.getType()==2){//油费
                    oilCostTotal = oilCostTotal+order.getTotal();
                }else if(order.getType()==3){//其他支出
                    otherCostTotal=otherCostTotal+order.getTotal();
                }

                if(dayOrders.containsKey(order.getTransferDate())){
                    dayOrders.get(order.getTransferDate()).add(order);
                }else{
                    List<Order> dayOrderLists= Lists.newArrayList();
                    dayOrderLists.add(order);
                    dayOrders.put(order.getTransferDate(),dayOrderLists);
                }
            }

//            total=repairCosts+transportation;
            result.put("repairCosts",repairCosts);
            result.put("transportation",transportation);
            result.put("receivedTotal",receivedTotal);
            result.put("advanceTotal",advanceTotal);
            result.put("oilCostTotal",oilCostTotal);
            result.put("otherCostTotal",otherCostTotal);
//            result.put("total",total);
            Set<String> keys=dayOrders.keySet();
            Iterator<String> it=keys.iterator();
            List<Map<String,Object>> monthOrders=Lists.newArrayList();
            while (it.hasNext()){
                String key=it.next();
                Map<String,Object> dayOrder=Maps.newHashMap();
                dayOrder.put("date",key);
                dayOrder.put("dayOrders",dayOrders.get(key));
                monthOrders.add(dayOrder);
            }
            result.put("monthOrders",monthOrders);
            return new Result<>(0,"success",result);
        }catch (Exception ex){
            logger.error("查询月份订单出现异常:{},date={},openId={}",ex,date,openId);
            return new Result<>(1,"fail");
        }
    }
}
